
#include "TestApp.h"


//--------------------------------------------------------------------------------------
// Called every time the application receives a message
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	PAINTSTRUCT ps;
	HDC hdc;

	switch( message )
	{
	case WM_PAINT:
		hdc = BeginPaint( hWnd, &ps );
		EndPaint( hWnd, &ps );
		break;

	case WM_DESTROY:
		PostQuitMessage( 0 );
		break;

	default:
		return DefWindowProc( hWnd, message, wParam, lParam );
	}

	return 0;
}



CTestApp::CTestApp()
{
	m_hInst = NULL;
	m_hWnd = NULL;

	m_pRenderSystem = NULL;
}


CTestApp::~CTestApp()
{

}


void CTestApp::onEvent(MSG& msg)
{

}


bool CTestApp::updateFrame(int passedTime)
{
	Render();
	static int FPS = 0;
	static int tempFPSTime = 0;
	FPS++;
	int nowtime = GetTickCount();
	int CurFPS = 0;
	if (nowtime - tempFPSTime >= 1000)
	{
		CurFPS = FPS;
		tempFPSTime = nowtime;
		FPS = 0;
		printf("--->FPS %d\n", CurFPS);

	}


	return true;
}


bool CTestApp::init( const char* appModuleFile )
{

	GetLaborCore()->loadPlugin( L"LaborD3D10D.dll");
	m_pRenderSystem = GetLaborCore()->getRenderSystemByName(L"D3D10 Renderer");
	GetLaborCore()->setRenderSystem( m_pRenderSystem );
	if ( NULL == m_pRenderSystem )
		return 0;


	if( FAILED( InitWindow( (HINSTANCE)m_pRenderSystem->getInst(), 0 ) ) )
		return 0;

	char _appModuleFile[256]={0};
	strncpy_s(_appModuleFile,appModuleFile,256);
	for(int i = strlen(_appModuleFile) ; i >=0 ; i --)
	{
		if(_appModuleFile[i] == '/' || _appModuleFile[i] == '\\')
		{
			_appModuleFile[i + 1] = 0;
			break;
		}
	}

	string t_strTemp = _appModuleFile;
	t_strTemp = t_strTemp.substr(0, t_strTemp.length()-1);
	int t_nPos = t_strTemp.find_last_of('\\');
	string t_strResourcePath = t_strTemp.substr(0, t_nPos+1);
	wchar_t t_strResourcePathW[4096] = {0};
	Labor_LocaleToUnicode( t_strResourcePath.c_str() ,t_strResourcePathW , 4096 );
	wstring t_strShaderPath = t_strResourcePathW;


	t_strShaderPath += L"resource\\shader\\";
	m_strShaderPath = t_strShaderPath;
	m_pRenderSystem->setShaderPath( t_strShaderPath.c_str() );

	wstring t_strTexturePath = t_strResourcePathW;

	t_strTexturePath += L"resource\\texture\\";
	m_pRenderSystem->setTexturePath( t_strTexturePath.c_str() );


	if( !m_pRenderSystem->initDevice( (int)m_hWnd) )
	{
		m_pRenderSystem->cleanUpDevice();
		return 0;
	}
	initRenderData();
	return true;

}


bool CTestApp::uinit()
{

	m_pRenderSystem->cleanUpDevice();

	return true;

}

HRESULT CTestApp::InitWindow( HINSTANCE hInstance, int nCmdShow )
{
	// Register class
	WNDCLASS wcex;
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon( hInstance, ( LPCTSTR )IDI_APPLICATION );
	wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
	wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = L"Test2DRender";
	if( !RegisterClass( &wcex ) )
		return E_FAIL;

	// Create window
	m_hInst = hInstance;
	RECT rc = { 0, 0, 640, 480 };
	AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
	m_hWnd = CreateWindow( L"Test2DRender", L"Test2DRender: Test Render", WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
		NULL );
	if( !m_hWnd )
		return E_FAIL;

	ShowWindow( m_hWnd, TRUE );

	return S_OK;
}


void CTestApp::initRenderData()
{

	m_pRenderSystem->setInputLayout( IL_PT );
	ILGpuProgram *pGpuProgram = m_pRenderSystem->createGPUProgram();
	wstring strVS, strPS;
	strVS = m_strShaderPath;
	strPS = m_strShaderPath;

	strVS += L"basic2D_Vertex.hlsl";
	strPS += L"basic2D_Pixel.hlsl";
	pGpuProgram->load( strVS.c_str(), strPS.c_str(), NULL, 0);
	m_pRenderSystem->setGPUProgram( pGpuProgram );
	HRESULT hr = S_OK;
	// Create vertex buffer


	m_pTexture = m_pRenderSystem->createTextureFromFile(L"seafloor.dds");
	m_pRenderSystem->setTexture( m_pTexture );

	//// sample state 
	sLSamplerStateDesc _sampler_desc;
	ILSamplerState *pSamplerState = m_pRenderSystem->createSamplerState( _sampler_desc );
	m_pRenderSystem->setSamplerState( 0, pSamplerState );


	pGpuProgram->commit();

}


//--------------------------------------------------------------------------------------
// Render the frame
//--------------------------------------------------------------------------------------
void CTestApp::Render()
{
	if ( NULL == m_pRenderSystem )
		return;
	m_pRenderSystem->clearBackBuffer(0.0f, 0.125f, 0.3f, 1.0f);
	m_pRenderSystem->begin();
	m_pRenderSystem->set2DZValue( 1000.0f );
	//m_pRenderSystem->begin2D();
	m_pRenderSystem->beginPerspective2D();
	m_pRenderSystem->drawRect( m_pTexture, CLRectF(0, 0, 200, 200), CLColor_4f(1.0, 1.0, 1.0, 1.0) );
	m_pRenderSystem->end2D();
	m_pRenderSystem->present(0, 0);
	m_pRenderSystem->end();
}
